﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bank
{
    // 普通账户类
    public class Account
    {
        protected double money;
        protected string account;
        protected string password;
        private static Random random = new Random();

        public event EventHandler<BigMoneyArgs> BigMoneyFetched;

        public Account(string account, string password)
        {
            this.account = account;
            this.password = password;
            money = 0;
        }

        public double getMoney()
        {
            return money;
        }

        // 用户对象存款方法
        public bool SaveMoney(double money)
        {
            if (money < 0) return false;

            if (random.NextDouble() < 0.4)
            {
                throw new BadCashException();
            }

            this.money += money;
            return true;
        }

        // 用户对象取款方法
        public virtual bool WithdrawMoney(double money)
        {
            if (this.money >= money)
            {
                this.money -= money;

                // 如果取款金额超过10000元，触发大额取款事件
                if (money >= 10000)
                {
                    OnBigMoneyFetched(new BigMoneyArgs(account, money));
                }

                return true;
            }
            return false;
        }

        // 用户对象登录方法
        public bool login(string account, string password)
        {
            return this.account == account && this.password == password;
        }

        protected virtual void OnBigMoneyFetched(BigMoneyArgs e)
        {
            BigMoneyFetched?.Invoke(this, e);
        }
    }

    // 信用账户类
    public class CreditAccount : Account
    {
        double credit;

        public CreditAccount(string account, string password, double credit) : base(account, password)
        {
            this.credit = credit;
        }

        public CreditAccount(string account, string password) : this(account, password, 10000) { }

        public override bool WithdrawMoney(double money)
        {
            if (this.money + credit >= money)
            {
                this.money -= money;

                // 如果取款金额超过10000元，触发大额取款事件
                if (money >= 10000)
                {
                    OnBigMoneyFetched(new BigMoneyArgs(account, money));
                }

                return true;
            }
            return false;
        }

        public double getCredit()
        {
            return credit;
        }
    }
}
